using System.Collections.Generic;
using DataAccess.Services.Interfaces;
using DataControl;
using NHibernate;

namespace DataAccess.Services.Gateways
{
	class UserGateway : GatewayBase<User>, IUserGateway
	{
		public UserGateway(ISession session) : base(session)
		{
		}

		public bool Exists(int clientId)
		{
			return DataControl.ClientExists(clientId);
		}

		public bool Exists(string login)
		{
			return DataControl.ClientExists(login);
		}

		public User GetByAccountCode(string accountCode)
		{
			return DataControl.GetClientByAccount(accountCode);
		}

		public int? EnsureUserCredentials(string login, string password)
		{
			return DataControl.EnsureClientCredentials(login, password);
		}

		public string GetUserPublicKey(int clientId)
		{
			return DataControl.GetPublicKeyByClientId(clientId);
		}

		public IEnumerable<int> GetUserRoles(int clientId)
		{
			return DataControl.GetClientRoles(clientId);
		}

		public IList<int> GetRoles()
		{
			return DataControl.GetRoles();
		}

		public IList<User> GetAllUsers()
		{
			return DataControl.GetAllItems<User>(); 
		}

		public void AssignRole(int userId, int roleId)
		{
			DataControl.Add(new Bnk_user_role {Id_bnk_user = userId, Id_user_role = roleId});
		}

		public void DeleteRole(int userId, int roleId)
		{
			DataControl.Delete<Bnk_user_role>(
				new List<KeyValuePair<string, string>>
					{
						new KeyValuePair<string, string>("Id_bnk_user", userId.ToString()),
						new KeyValuePair<string, string>("Id_user_role", roleId.ToString())
					});
		}

		public void DeleteAllRoles(int userId)
		{
			DataControl.Delete<Bnk_user_role>("Id_bnk_user", userId.ToString());
		}
	}
}